# # -*- coding: utf-8 -*-
# """
# 七牛云文件上传工具类
# 封装了七牛云对象存储的上传功能
# """
#
# from qiniu import Auth, put_file_v2, etag
# import os
# from typing import Optional, Dict, Any
#
#
# class QiniuUploader:
#     """七牛云文件上传工具类"""
#
#     def __init__(self, access_key: str, secret_key: str, bucket_name: str):
#         """
#         初始化七牛云上传工具
#
#         Args:
#             access_key: 七牛云 Access Key
#             secret_key: 七牛云 Secret Key
#             bucket_name: 要上传的空间名称
#         """
#         self.access_key = access_key
#         self.secret_key = secret_key
#         self.bucket_name = bucket_name
#         self.q = Auth(access_key, secret_key)
#
#     def upload_file(self, local_file_path: str, key: Optional[str] = None,
#                    expires: int = 3600) -> Dict[str, Any]:
#         """
#         上传文件到七牛云
#
#         Args:
#             local_file_path: 本地文件路径
#             key: 上传后保存的文件名，如果为None则使用原文件名
#             expires: 上传凭证过期时间（秒）
#
#         Returns:
#             包含上传结果的字典
#             成功时: {"success": True, "key": key, "hash": hash, "info": info}
#             失败时: {"success": False, "error": error_message, "info": info}
#         """
#         try:
#             # 检查本地文件是否存在
#             if not os.path.exists(local_file_path):
#                 return {
#                     "success": False,
#                     "error": f"本地文件不存在: {local_file_path}",
#                     "info": None
#                 }
#
#             # 如果没有指定key，使用原文件名
#             if key is None:
#                 key = os.path.basename(local_file_path)
#
#             # 生成上传 Token
#             token = self.q.upload_token(self.bucket_name, key, expires)
#
#             # 执行上传
#             ret, info = put_file_v2(token, key, local_file_path, version='v2')
#
#             # 检查上传结果
#             if ret is not None and info.status_code == 200:
#                 # 验证文件hash
#                 expected_hash = etag(local_file_path)
#                 if ret['hash'] == expected_hash:
#                     return {
#                         "success": True,
#                         "key": ret['key'],
#                         "hash": ret['hash'],
#                         "info": info
#                     }
#                 else:
#                     return {
#                         "success": False,
#                         "error": "文件hash校验失败",
#                         "info": info
#                     }
#             else:
#                 return {
#                     "success": False,
#                     "error": "上传失败",
#                     "info": info
#                 }
#
#         except Exception as e:
#             return {
#                 "success": False,
#                 "error": f"上传过程中发生异常: {str(e)}",
#                 "info": None
#             }
#
#     def generate_upload_token(self, key: Optional[str] = None, expires: int = 3600) -> str:
#         """
#         生成上传凭证，可用于前端直传
#
#         Args:
#             key: 上传后保存的文件名
#             expires: 上传凭证过期时间（秒）
#
#         Returns:
#             上传凭证字符串
#         """
#         return self.q.upload_token(self.bucket_name, key, expires)
#
#     def get_file_url(self, key: str, domain: str) -> str:
#         """
#         获取文件访问URL
#
#         Args:
#             key: 文件名
#             domain: 空间对应的域名
#
#         Returns:
#             文件访问URL
#         """
#         return f"http://{domain}/{key}"
#
#
# # 使用示例
# if __name__ == "__main__":
#     # 配置信息
#     ACCESS_KEY = '-YblO8MOdeJTLkQBTL8BZ0mevKdCy04sWwGfVFJJ'
#     SECRET_KEY = 'PqGsNCT5c__FyRp8Pxe5FyX4sonjevty3BH-esKi'
#     BUCKET_NAME = 'your_bucket_name'
#
#     # 创建上传工具实例
#     uploader = QiniuUploader(ACCESS_KEY, SECRET_KEY, BUCKET_NAME)
#
#     # 上传文件
#     result = uploader.upload_file('./sync/bbb.jpg', 'my-python-logo.png')
#
#     if result["success"]:
#         print(f"上传成功: {result}")
#     else:
#         print(f"上传失败: {result['error']}")